package com.dotflat.photoAlbum.service.impl;


import com.dotflat.photoAlbum.domain.Photo;
import com.dotflat.photoAlbum.domain.PhotoAlbum;
import com.dotflat.photoAlbum.repository.PhotoAlbumRepository;
import com.dotflat.photoAlbum.repository.PhotoRepository;
import com.dotflat.photoAlbum.service.PhotoService;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.repository.ClassesRepository;
import com.starinside.wp.repository.FeedsRepository;
import com.starinside.wp.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


@Service
public class PhotoServiceImpl implements PhotoService {

    @Autowired
    PhotoAlbumRepository albumRepository;

    @Autowired
    PhotoRepository photoRepository;

    @Autowired
    ClassesRepository classRepository;

    @Autowired
    UploadService uploadService;

    @Autowired
    FeedsRepository feedsRepository;

    @Override
    public Page<Photo> list(String areaId, String childrenId, PagerQuery pagerQuery) {

        PhotoAlbum album = albumRepository.getOneByAreaIdAndChildrenId(areaId, childrenId);

        if (album == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "相册不存在" );
        }
        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize(),
                Sort.by("created").descending());

        return photoRepository.findAllByDeletedIsFalseAndAlbumId(album.getId(), pageRequest);
    }

}
